'''
@Author: ysz
@Date: 2019-12-18 15:11:26
@LastEditors  : ysz
@LastEditTime : 2020-01-07 09:31:21
@Description: 对代码进行解码操作
'''

from fontTools.ttLib import TTFont
import base64
import io
import re

class Jiemi(object):

    #使用字典批量替换
    def multReplace(self,text, rpdict):
        rx = re.compile('|'.join(map(re.escape, rpdict)))
        return rx.sub(lambda match:rpdict[match.group(0)], text)

    #解密58房产的字体加密
    def decode58Fangchan(self,html,key):
        glyphdict = {
            'glyph00001': '0',
            'glyph00002': '1',
            'glyph00003': '2',
            'glyph00004': '3',
            'glyph00005': '4',
            'glyph00006': '5',
            'glyph00007': '6',
            'glyph00008': '7',
            'glyph00009': '8',
            'glyph00010': '9'
        }    
        data = base64.b64decode(key)  #base64解码
        fonts = TTFont(io.BytesIO(data))  #生成二进制字节
        cmap = fonts.getBestCmap()  #十进制ascii码到字形名的对应{38006:'glyph00002',...}
        chrMapNum = {}  #将变为{‘龥’:'1',...}
        for asc in cmap:
            chrMapNum[chr(asc)] = glyphdict[cmap[asc]]
        return self.multReplace(html,chrMapNum)